package it.polimi.traveldream.web.beans.employee;

import it.polimi.traveldream.ejbclient.dtos.TravelPackageDTO;
import it.polimi.traveldream.ejbclient.eaos.TravelPackageServiceEAO;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;

@ManagedBean(name="listTravelPackages")
@SessionScoped
public class ListPackages {
	@EJB
	private TravelPackageServiceEAO tService;
	private List<TravelPackageDTO> travelPackages;
	private TravelPackageDTO selectedTravelPackage;
	@ManagedProperty(value="#{personalHomeEmployee}")
	private PersonalHomeEmployeeBean employeeBean;
	
	public ListPackages() {
	}
	
	@PostConstruct
	public void init() {
		travelPackages = tService.getPackages(employeeBean.isTravelPackageOverpast());
	}

	public void setEmployeeBean(PersonalHomeEmployeeBean employeeBean) {
		this.employeeBean = employeeBean;
	}

	public List<TravelPackageDTO> getTravelPackages() {
		return travelPackages;
	}

	public void setTravelPackages(List<TravelPackageDTO> travelPackages) {
		this.travelPackages = travelPackages;
	}

	public TravelPackageDTO getSelectedTravelPackage() {
		return selectedTravelPackage;
	}

	public void setSelectedTravelPackage(TravelPackageDTO selectedTravelPackage) {
		this.selectedTravelPackage = selectedTravelPackage;
	}

	public void onTravelPackageRowSelectNavigate(SelectEvent event) {
		FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedTravelPackage", event.getObject());  
		ConfigurableNavigationHandler nh = (ConfigurableNavigationHandler)FacesContext.getCurrentInstance().getApplication().getNavigationHandler();
		nh.performNavigation("detailTravelPackage?faces-redirect=true"); 
    }
	
}
